package com.weapons.week0903_weapons.fragment;


import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.activity.FeedbackActivity;
import com.weapons.week0903_weapons.utils.DataCleanManage;

import java.io.File;


/**
 * A simple {@link Fragment} subclass.
 */
public class WeaponsFragment extends Fragment {
    private Activity mActivity;
    private String mpos="";
    private PopupWindow popupWindow;
    private TextView tv;
    private TextView tvSize;
    private TextView tvFeedback;
    private TextView tvClear;
    private TextView tvClearCache;
    private float count;
    private String[] textSize={"小","中","大","超大"};

    public WeaponsFragment newInstance(String pos) {
        WeaponsFragment fragment=new WeaponsFragment();
        Bundle bundle=new Bundle();
        bundle.putString("pos",pos);
        fragment.setArguments(new Bundle(bundle));
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        popupWindow=new PopupWindow();
        mActivity=getActivity();
        Bundle bundle=getArguments();
        if (bundle!=null){
            mpos=bundle.getString("pos");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_weapons, container, false);
    }


    //实现浮动框的弹出，输入法的动态调用
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final Toolbar toolbar= (Toolbar) view.findViewById(R.id.weapons_search_toolbar);
        final LinearLayout ll= (LinearLayout) view.findViewById(R.id.weapons_fragment__search_container);
        tv= (TextView) view.findViewById(R.id.tv_textsize_weapons);
        tvSize= (TextView) view.findViewById(R.id.tv_textsize_change);
        tvFeedback= (TextView) view.findViewById(R.id.tv_weapons_feedback);
        tvClear= (TextView) view.findViewById(R.id.tv_clear);
        tvClearCache= (TextView) view.findViewById(R.id.tv_clearCache);


        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出浮动框
                //封装代码（以LinearLayout作为参照，所以作为入参传递）
                showPopUpWindow(ll,toolbar);

                //输入输入法
                InputMethodManager imm= (InputMethodManager) getActivity().getSystemService(Service.INPUT_METHOD_SERVICE);
                //显示/隐藏
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });


        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(mActivity);
                builder.create().show();
                builder.setTitle("正文字体").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(textSize, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        tvSize.setText(textSize[which]);
                    }
                }).setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //确定按钮
                    }
                }).setNegativeButton("否",null).show();
            }
        });

        tvFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity,FeedbackActivity.class));
            }
        });


//        tvClearCache.setText(count+"M");
        tvClearCache.setText(count+"M");
        tvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String filePath="/Pictures/";

                Log.i("filePath","------------->"+filePath);
                DataCleanManage.cleanCustomCache(filePath);


            }
        });
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void showPopUpWindow(LinearLayout ll,Toolbar toolbar){
        if (!popupWindow.isShowing()){
            popupWindow.setWidth(toolbar.getWidth());
            popupWindow.setHeight(toolbar.getHeight());
            View view=getActivity().getLayoutInflater().inflate(R.layout.popupwindow_search,null);
            TextView tvCancel= (TextView) view.findViewById(R.id.tv_cancel);
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //认为取消浮动窗口
                    popupWindow.dismiss();
                }
            });
            popupWindow.setContentView(view);
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);
//            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.showAtLocation(ll, Gravity.TOP,0,getStatusHeight());//Y偏移状态栏的高度
        }
    }
    /**
     * 获取状态栏的高度
     * @return
     */
    public int getStatusHeight() {
        int height=0;
        int resId;
        resId=getResources().getIdentifier("status_bar_height","dimen","android");
        if (resId>0){
            height=getResources().getDimensionPixelOffset(resId);//通过地址获取高度
        }
        return height;
    }


}
